package com.link.base.tencent.corpwx.usermanage.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.link.base.tencent.corpwx.addressbook.model.User;
import com.link.base.tencent.corpwx.addressbook.service.AddressBookUserService;
import com.link.base.tencent.corpwx.agent.service.AgentService;
import com.link.base.tencent.corpwx.agent.util.AgentIdUtil;
import com.link.base.tencent.corpwx.externalcontact.util.ExternalContactUtil;
import com.link.base.tencent.corpwx.usermanage.dao.mybatis.mapper.CorpWxUserMapper;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceException;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.util.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * 企业用户-企业微信通讯录中的内部用户管理
 *
 * @author 李宗庭
 * @date 2019/02/27 16:04
 */
@Service
public class CorpWxUserServiceImpl extends BasicServiceImpl<User> implements CorpWxUserService {

    @Resource
    private CorpWxUserMapper corpWxUserMapper;
    @Resource
    private AddressBookUserService addressBookUserService;
    @Resource
    private AgentService agentService;

    @Override
    public BasicMapper<User> getBasicMapper() {
        return corpWxUserMapper;
    }

    @Override
    public User queryByUsername(String username) throws Exception {
        if (StringUtils.isBlank(username)) {
            throw new BasicServiceException("username不能为空！");
        }
        return corpWxUserMapper.queryByUsername(username);
    }

    @Override
    public void synCorpWxUserToWx(User user, String agentId) throws Exception {
        if (StringUtils.isBlank(user.getUserId())) {
            throw new BasicServiceException("userId不能为空！");
        }
        user = queryByUsername(user.getUserId());
        //目前暂时设置所有用户的部门为根部门
        user.setDepartment("1");
        addressBookUserService.synUser(user, agentId);
    }

    @Override
    public String synCorpWxUsersToWx(String agentId) throws Exception {
        User user = new User();
        List<User> list = corpWxUserMapper.queryAll(user);
        //构造生成excel的表头
        LinkedHashMap map = new LinkedHashMap();
        map.put("name", "姓名");
        map.put("userId", "账号");
        map.put("mobile", "手机号");
        map.put("email", "邮箱");
        map.put("department", "所在部门");
        map.put("position", "职位");
        map.put("gender", "性别");
        map.put("isLeaderInDept", "是否领导");
        map.put("order", "排序");
        map.put("alias", "别名");
        map.put("address", "地址");
        map.put("telephone", "座机");
        map.put("enable", "禁用");
        return addressBookUserService.batchUpdateUsers(list, map,agentId,false);
    }

    @Override
    @Transactional(rollbackFor = BasicServiceException.class)
    public void synCorpWxFollowUserToCRM() throws Exception {
        String accessToken = agentService.getExistAccessToken(AgentIdUtil.CORPWX_ACCOUNT_AGENTID);
        // 获取配置了客户联系功能的成员Id列表
        JSONObject followUser = ExternalContactUtil.getFollowUserList(accessToken);
        JSONArray followUserList = followUser.getJSONArray("follow_user");
        if (followUserList != null) {
            // 初始化用户的客户联系功能标识字段
            corpWxUserMapper.initFollowFlag();
            List<String> list = followUserList.toJavaList(String.class);
            corpWxUserMapper.setFollowFlags(list);
        }
    }

    @Override
    public List<User> corpWxUserList(User user) throws Exception {
        return corpWxUserMapper.corpWxUserList(user);
    }
}

